package login.dao;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.DaoCommonHandler;
import game.db.DataSourceManager;

/**
 * 玩家货币消耗Dao
 * 
 * @author zhangning
 * 
 * @Date 2015年5月27日 下午7:29:28
 */
public class LogIdfaDao {

	private static Logger logger = LoggerFactory.getLogger(LogIdfaDao.class);

	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	static class SingletonHolder {
		static LogIdfaDao instance = new LogIdfaDao();
	}

	public static LogIdfaDao getInstance() {
		return SingletonHolder.instance;
	}

	/**
	 * 批量插入货币收益日志
	 * 
	 * @param logCosts
	 * @return
	 */
	public void addLogIdfa(String idfaStr) {
		QueryRunner runner = new QueryRunner(dataSource);
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			runner.update(conn, "insert into login_idfa_log(Idfa,create_time) values(?,NOW())", idfaStr);
		} catch (SQLException e) {
			logger.error("", e);
		} finally {
			DbUtils.closeQuietly(conn);
		}
	}

	public int getUniqueIdfaCount(String queryDate) {
		int ret = 0;
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query(
					"SELECT count(distinct(Idfa)) from login_idfa_log where create_time > ? and create_time < ?",
					DaoCommonHandler.Integerhandler, queryDate + " 00:00:00", queryDate + " 23:59:59");
		} catch (SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;
	}

}
